//
//  ZRPublishPhotoView.swift
//  Sina
//
//  Created by 张锐 on 16/3/7.
//  Copyright © 2016年 zhangr. All rights reserved.
//

import UIKit

let collectionViewMargin = 10

let collectionItemMargin = 5

let ZRPublishPhotoViewReuseIdentifier = "ZRPublishPhotoViewReuseIdentifier"



class ZRPublishPhotoView: UICollectionView,UICollectionViewDataSource,ZRPublishPhotoCellDelegate,UICollectionViewDelegate {
    
    /// 定义回调闭包 用于加好cell点击
    var closure : (()->())?
    
    var images : [UIImage] = [UIImage]()

    var flowLayout : UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    
    //重写构造方法
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    //setupUI
    
    func setupUI(){
        
        backgroundColor = UIColor.lightGrayColor()
    
        self.dataSource = self
        self.delegate = self
        
        registerClass(ZRPublishPhotoCell.self, forCellWithReuseIdentifier: ZRPublishPhotoViewReuseIdentifier)
        
        let itemWH = (UIScreen.mainScreen().bounds.width - CGFloat(2 * (collectionItemMargin + collectionViewMargin))) / 3
        
        flowLayout.itemSize = CGSizeMake(itemWH, itemWH)
        
        flowLayout.minimumInteritemSpacing = CGFloat(collectionItemMargin)
        
        flowLayout.minimumLineSpacing = CGFloat(collectionItemMargin)
    
    }
    
    /**
     提供外部增加本对象图片数组数据的方法
     
     - parameter image: 传入的image
     */
    func addImage(image: UIImage){
    
        images.append(image)

        reloadData()
    
    }
//    deinit{
//        
//        print("photo88")
//        
//    }
    

}
// MARK: - collection数据源方法 cell的delegate方法
extension ZRPublishPhotoView{

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if images.count == 0 || images.count == 9{
        
            return images.count
        }else{
        
            return images.count + 1
        }
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ZRPublishPhotoViewReuseIdentifier, forIndexPath: indexPath) as! ZRPublishPhotoCell
        
        if indexPath.item == images.count {
        
            cell.image = UIImage(named: "compose_pic_add_highlighted")
            
            cell.backgroundColor = UIColor.orangeColor()
            
            cell.deleteButton.hidden = true
        
        }else{
            
        cell.image = images[indexPath.item]
            
        cell.deleteButton.hidden = false
        
        cell.deleteMark = indexPath.item
        
        cell.delegate = self}
        
        return cell
    }
    /**
     cell的delegate方法
     */
    func deleteButtonDidClickd(mark : Int){
    
        images.removeAtIndex(mark)
        
        reloadData()
    
    }
    /**
    *  点击加好cell
    */
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        if indexPath.item == images.count{
        
        
            closure!()
        
        }
    }

}

/**
 *  回调协议
 */
protocol ZRPublishPhotoCellDelegate : NSObjectProtocol{

    func deleteButtonDidClickd(mark : Int)

}

/// item类
class ZRPublishPhotoCell : UICollectionViewCell{
    
    var deleteMark : Int = 0
    
    weak var delegate : ZRPublishPhotoCellDelegate?
    
    var image : UIImage?{
    
        didSet{
        
        iconImageView.image = image
        
        }
    }

    //重写构造方法
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //setupUI
    
    private func setupUI(){
    
        addSubview(iconImageView)
        
        addSubview(deleteButton)
        
        //设置约束
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.snp_top)
            
            make.left.equalTo(self.snp_left)
            
            make.right.equalTo(self.snp_right)
            
            make.bottom.equalTo(self.snp_bottom)
        }
        
        deleteButton.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.snp_top)
            
            make.right.equalTo(self.snp_right)
        }
    
    
    }
    
    //懒加载


    lazy var iconImageView : UIImageView = UIImageView()
    
    lazy var deleteButton : UIButton = {
    
        let deleteButton = UIButton(type: UIButtonType.Custom)
        
        deleteButton.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        
        deleteButton.addTarget(self, action: "deleteButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        deleteButton.sizeToFit()
        
        return deleteButton
    
    }()
    
    /**
    *  删除按钮点击事件
    */
    func deleteButtonClick(){
    
        if (delegate != nil) {
        
            delegate?.deleteButtonDidClickd(deleteMark)
        
        }
    
    }

}
